//
//  HMUserAccountModel.swift
//  WB25
//
//  Created by HM on 2017/7/1.
//  Copyright © 2017年 itheima. All rights reserved.
//

import UIKit

class WBUserAccountModel: NSObject, NSCoding{
    /// 用户授权的唯一票据
    var access_token: String?
    /// access_token的生命周期，单位是秒数,通过didset方法监听给expires_in赋值时做判断
    var expires_in: Double = 0{
        didSet{
            expires_Date = Date(timeIntervalSinceNow: expires_in)
        }
    }
    /// 授权用户的UID
    var uid: String?
    /// 用户昵称
    var screen_name:String?
    /// 用户头像
    var avatar_large: String?
    // 用来判断用户token过期时间(普通用户3天  会员5年)
    var expires_Date:Date?
    // KVC构造函数
    init(dict:[String: Any]) {
        super.init()
        setValuesForKeys(dict)
    }
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        
    }
    
    //
    override var description: String{
        // keys
        let keys = ["access_token", "expires_Date", "uid", "screen_name", "avatar_large"]
        return dictionaryWithValues(forKeys: keys).description
    }
    // 归档 ->属性存二进制保存到磁盘
    func encode(with aCoder:NSCoder){
        aCoder.encode(access_token, forKey: "access_token")
        aCoder.encode(expires_Date, forKey: "expires_Date")
        aCoder.encode(uid, forKey: "uid")
        aCoder.encode(screen_name, forKey: "screen_name")
        aCoder.encode(avatar_large, forKey: "avatar_large")
    }
    // 解档 -> 从磁盘中获取二进制变成属性
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObject(forKey: "access_token") as? String
        expires_Date = aDecoder.decodeObject(forKey: "expires_Date") as? Date
        uid = aDecoder.decodeObject(forKey: "uid") as? String
        screen_name = aDecoder.decodeObject(forKey: "screen_name") as? String
        avatar_large = aDecoder.decodeObject(forKey: "avatar_large") as? String
    }
}
